package com.example.azheng.rxjavamvpdemo.ui.acitivity;

import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.azheng.rxjavamvpdemo.R;
import com.example.azheng.rxjavamvpdemo.base.BaseMvpActivity;
import com.example.azheng.rxjavamvpdemo.bean.BaseObjectBean;
import com.example.azheng.rxjavamvpdemo.mvp.contract.ServiceContract;
import com.example.azheng.rxjavamvpdemo.mvp.presenter.ServicePresenter;

import butterknife.BindView;
import butterknife.OnClick;

public class ServiceActivity extends BaseMvpActivity<ServicePresenter> implements ServiceContract.View {


    @BindView(R.id.iv_back_toolbar)
    ImageView ivBackToolbar;
    @BindView(R.id.tv_title_toolbar)
    TextView tvTitleToolbar;
    @BindView(R.id.wbService)
    WebView wbService;

    @Override
    public int getLayoutId() {
        return R.layout.activity_service;
    }

    @Override
    public void initView() {
        tvTitleToolbar.setText("客服信息");
        mPresenter = new ServicePresenter();
        mPresenter.attachView(this);
        mPresenter.service();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void serviceSuccess(BaseObjectBean<String> bean) {
        String data = bean.getData();
        loadWebView(data);
    }

    /**
     * 加载k线图
     */
    private void loadWebView(String html) {

        //声明WebSettings子类
        WebSettings webSettings = wbService.getSettings();
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);

        //设置自适应屏幕，两者合用
        //将图片调整到适合webview的大小
        webSettings.setUseWideViewPort(true);
        // 缩放至屏幕的大小
        webSettings.setLoadWithOverviewMode(true);

        //缩放操作
        //支持缩放，默认为true。是下面那个的前提。
        webSettings.setSupportZoom(true);
        //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setBuiltInZoomControls(true);
        //隐藏原生的缩放控件
        webSettings.setDisplayZoomControls(false);

        //其他细节操作
        //关闭webview中缓存
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //支持通过JS打开新窗口
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        //支持自动加载图片
        webSettings.setLoadsImagesAutomatically(true);
        //设置编码格式
        webSettings.setDefaultTextEncodingName("utf-8");
        //设置不要跳转到浏览器
        wbService.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        wbService.loadData(html, "text/html", "UTF-8");

    }

    @OnClick(R.id.iv_back_toolbar)
    public void onViewClicked() {
        finish();
    }
}